<?php
/**
 * Tratamento de Views
 *
 *
 *    @filesource     $HeadURL: http://oraculum-php.googlecode.com/svn/branches/2014j1/core/general/Views.php $
 *    @category       Framework
 *    @package        oraculum
 *    @subpackage     oraculum.core.views
 *    @license        http://www.opensource.org/licenses/lgpl-3.0.html (LGPLv3)
 *    @version        $Revision: 120 $
 *    @modifiedby     $LastChangedBy: patrixsbs $
 *    @lastmodified   $Date: 2014-01-06 13:17:28 +0000 (Mon, 06 Jan 2014) $
 *
 */

	class Oraculum_Views {
		protected $_ViewsRegister=NULL;
		protected $_template=NULL;
		protected $_templatetype='xml';
		protected $_pagefile=NULL;

		public function __construct() {
			if (!defined('VIEW_DIR')):
				define('VIEW_DIR', 'views');
			endif;
			$this->_ViewsRegister=new Oraculum_ViewsRegister();
		}

		public function AddTemplate($template=NULL) {
			if (is_null($template)):
				throw new Exception ('[Erro CGV29] Template nao informado');
			else:
				$templatefilexml=VIEW_DIR.'/templates/'.$template.'.xml';
				$templatefilephp=VIEW_DIR.'/templates/'.$template.'.php';
				if(file_exists($templatefilexml)):
					$this->_template=simplexml_load_file($templatefilexml);
					$this->_templatetype='xml';
				elseif(file_exists($templatefilephp)):
					$this->_template=file_get_contents($templatefilephp);
					$this->_template=explode('[content]',$this->_template);
					$this->_templatetype='html';
				else:
					throw new Exception('[Erro CGV37] Template nao encontrado ('.$template.') ');
				endif;
			endif;      
			$this->_ViewsRegister=$this->_ViewsRegister->AddTemplate($template);
			return $this;
		}

		public function LoadPage($page=NULL) {
			if (is_null($page)):
				throw new Exception ('[Erro CGV47] Pagina nao informada');
			else:
				$pagefile=VIEW_DIR.'/pages/'.$page.'.php';
				$class=ucwords($page).'View';
				if (file_exists($pagefile)):
					$templates=$this->_ViewsRegister->GetTemplates();
					if (empty($templates)):
						include_once($pagefile);
					else:
						if ($this->_templatetype=='xml'):
							$this->_pagefile=$pagefile;
							echo $this->_RenderXML();
						else:
							echo $this->_template[0];
							include_once($pagefile);
							echo $this->_template[1];
							return $this;
						endif;
					endif;
				else:
					throw new Exception('[Erro CGV61] Pagina nao encontrada ('.$pagefile.') ');
				endif;
				if (class_exists($class)):
					new $class;
				endif;
			endif;
			return $this;
		}
		
		private function _RenderXML() {
			foreach($this->_template as $item=>$value):
				if ($item=='content'):
					include_once($this->_pagefile);
				elseif($item=='text'):
					echo $value;
				elseif($item=='element'):
					$this->LoadElement($value);
				endif;
			endforeach;
		}

		public static function LoadElement($element=NULL) {
			if (is_null($element)):
				throw new Exception ('[Erro CGV69] Elemento nao informado');
			else:
				$elementfile=VIEW_DIR.'/elements/'.$element.'.php';
				if (file_exists($elementfile)):
					include_once($elementfile);
				else:
					throw new Exception('[Erro CGV75] Elemento nao encontrado ('.$elementfile.') ');
				endif;
			endif;
		}
	}

	class  Oraculum_ViewsRegister
	{
		protected $_templates=array();
		
		public function AddTemplate($template) {
			$this->_templates[]=$template;
			return $this;
		}
		
		public function GetTemplates() {
			return $this->_templates;
		}
	}
